/*
    Copyright 2009 Spencer B. Koehler, Ph.D.

    This file is part of the Brillig Toolkit.

    The Brillig Toolkit is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Brillig Toolkit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with The Brillig Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.brillig.util;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class to test regex patterns against some text.
 * <p>
 * @author Spence Koehler
 */
public class Regex {

  /**
   * Usage: java org.....Regex pattern input
   * i.e. java -ea Regex "(.+?)\s+(.+)" "this is a test"
   *      java -ea Regex "(.+)\s+(.+)" "this is a test"
   */
  public static void main(String[] args) {
    if (args.length != 2) {
      System.out.println("Usage: " + Regex.class.getName() + " patternString inputString");
      System.exit(-1);
    }

    Pattern p = Pattern.compile(args[0]);
    Matcher m = p.matcher(args[1]);

    if (m.matches()) {
      System.out.println("string \"" + args[1] + "\" matches pattern: \"" + args[0] + "\":");
      for (int i = 0; i <= m.groupCount(); i++) {
        System.out.println("group[" + i + "]='" + m.group(i) + "'");
      }
    }
    else {
      System.out.println("string \"" + args[1] + "\" does not match pattern: \"" + args[0] + '"');
    }
  }
}
